/*
 * Package il.ac.biu.cs.grossmm.api.data
 * File NodeSetByInterface.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.api.data;

/**
 * Represents a node-set of nodes of a given type within a given node type as
 * defined by a node-set constant in a node type interface.
 * <p>
 * Instances of this class are created using
 * {@link NodeTypeByInterface#nodeset(Class, Class)} method
 * 
 * @see NodeInterface
 * 
 * @author Mark Grossmann
 */
public class NodesetByInterface<N, S> implements Nodeset<N, S> {
	private int index;

	private NodeType<N> parentNodeType;

	private NodeType<S> nestedNodeType;

	private boolean initialzied = false;

	private Class<N> nClass;

	private Class<S> sClass;
	
	private String name;

	/**
	 * Constructs a new instance
	 * 
	 * @param nClass
	 * @param sClass
	 */
	NodesetByInterface(Class<N> nClass, Class<S> sClass)
	{
		super();
		this.nClass = nClass;
		this.sClass = sClass;
	}

	void initialize(int index, String name, NodeType<N> parentNodeType,
		NodeType<S> nestedNodeType)
	{
		this.index = index;
		this.name = name;

		this.parentNodeType = parentNodeType;
		this.nestedNodeType = nestedNodeType;

		this.initialzied = true;
	}

	private void ensureInit()
	{
		if (!initialzied)
			NodeTypeByInterface.initialize(nClass);
	}

	public int index()
	{
		ensureInit();

		return index;
	}

	public NodeType<S> nestedNodeType()
	{
		ensureInit();

		return nestedNodeType;
	}

	public NodeType<N> parentNodeType()
	{
		ensureInit();

		return parentNodeType;
	}

	public Class<N> getDeclaringInterface()
	{
		return nClass;
	}

	/**
	 * Returns Class object of nested node type interfeca
	 * 
	 * @return Class object of nested node type interfeca
	 */
	Class<S> getNestedInterface()
	{
		return sClass;
	}

	@Override
	public String toString()
	{
		return name;
	}
}
